package com.cb2.algorithm.leetcode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * <a href="https://leetcode.cn/problems/binary-tree-right-side-view/">二叉树的右视图(Binary Tree Right Side View)</a>
 * <p>给定一个二叉树的 根节点 root，想象自己站在它的右侧，按照从顶部到底部的顺序，返回从右侧所能看到的节点值。</p>
 * <p>
 * <b>示例：</b>
 * <pre>
 * 示例 1:
 *      输入: [1,2,3,null,5,null,4]
 *                  1       <=
 *                 / \
 *                2   3     <=
 *                 \   \
 *                  5   4   <=
 *      输出: [1,3,4]
 *
 * 示例 2:
 *      输入: [1,null,3]
 *      输出: [1,3]
 *
 * 示例 3:
 *      输入: []
 *      输出: []
 * </pre>
 * </p>
 * <p>
 * <b>提示:</b>
 * <ul>
 *     <li>二叉树的节点个数的范围是 [0,100]</li>
 *     <li>-100 <= Node.val <= 100 </li>
 * </ul>
 * </p>
 *
 * @author c2b
 * @since 2023/5/12 10:52
 */
public class LC0199BinaryTreeRightSideView_M {
    static class Solution {
        public List<Integer> rightSideView(TreeNode root) {
            //return rightSideViewByIterator(root);
            return rightSideViewByRecursion(root);
        }

        private List<Integer> rightSideViewByIterator(TreeNode root) {
            List<Integer> resList = new ArrayList<>();
            if (root == null) {
                return resList;
            }
            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root);
            while (!queue.isEmpty()) {
                int currLevelNodeSize = queue.size();
                for (int i = 0; i < currLevelNodeSize; i++) {
                    TreeNode currNode = queue.poll();
                    if (i == currLevelNodeSize - 1) {
                        resList.add(currNode.val);
                    }
                    if (currNode.left != null) {
                        queue.offer(currNode.left);
                    }
                    if (currNode.right != null) {
                        queue.offer(currNode.right);
                    }
                }
            }
            return resList;
        }

        private List<Integer> rightSideViewByRecursion(TreeNode root) {
            List<Integer> resList = new ArrayList<>();
            dfs(root, 0, resList);
            return resList;
        }

        private void dfs(TreeNode root, int depth, List<Integer> resList) {
            if (root == null) {
                return;
            }
            // 首次来到新的一层
            if (depth == resList.size()) {
                resList.add(root.val);
            }
            // 先右子树再左子树，保证首次遇到一定是最右边的节点
            dfs(root.right, depth + 1, resList);
            dfs(root.left, depth + 1, resList);
        }
    }

    public static void main(String[] args) {
        TreeNode root1 = new TreeNode(1);
        root1.left = new TreeNode(2);
        root1.right = new TreeNode(3);
        root1.left.right = new TreeNode(5);
        root1.right.right = new TreeNode(4);

        TreeNode root2 = new TreeNode(1);
        root2.right = new TreeNode(3);

        Solution solution = new Solution();
        Printer.printListInteger(solution.rightSideView(root1));
        Printer.printListInteger(solution.rightSideView(root2));
    }
}